package com.road2trip.restlet;

import java.io.IOException;

import org.restlet.data.MediaType;
import org.restlet.ext.xml.DomRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.Variant;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
 * Resource which has only one representation.
 * 
 */
public class PlacesResource extends ServerResource {

	 public PlacesResource() {  
	        super();  

	        // Here we add the representation variants exposed  
	        getVariants().add(new Variant(MediaType.TEXT_XML));  
	    }  
	 
	 	 
	 //http://www.restlet.org/documentation/2.0/gae/ext/
	 //http://www.restlet.org/documentation/1.1/firstResource
	 
	 	//@Override  
	    public Representation represent(Variant variant) {  
	       
	 	   if (variant.getMediaType().equals(MediaType.TEXT_XML)) {  
	           	    	  
	    	   try {  
	                DomRepresentation representation = new DomRepresentation(  
	                        MediaType.TEXT_XML);  
	                
	                Document d = representation.getDocument();  
	                Element r = d.createElement("places");  
	                d.appendChild(r);  
	                /*for (Item item : getItems().values()) {  
	                    Element eltItem = d.createElement("item");  
	  
	                    Element eltName = d.createElement("name");  
	                    eltName.appendChild(d.createTextNode(item.getName()));  
	                    eltItem.appendChild(eltName);  
	  
	                    Element eltDescription = d.createElement("description");  
	                    eltDescription.appendChild(d.createTextNode(item  
	                            .getDescription()));  
	                    eltItem.appendChild(eltDescription);  
	  
	                    r.appendChild(eltItem);  
	                }  */
	                d.normalizeDocument();  
	  
	                // Returns the XML representation of this document.  
	                return representation;  
	                
	            } catch (IOException e) {  
	                e.printStackTrace();  
	            }  
	            
	        }  
	       
	        return null;  
	    } 
	    
    @Get
    public String represent() {       
    	
    	return "hello, world (from the cloud!!!)";
    }

}